package org.zfes.snowier.cloud.zbss.layer.tc.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.ILAuthUserService;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.IPractitionerService;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.consts.BizConst;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.dao.PractitionerMapper;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.LAuthUser;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.Practitioner;

@Service
public class PractitionerServiceImpl implements IPractitionerService{
 	@Autowired
	private PractitionerMapper practitionerMapper;
	@Autowired
	private ILAuthUserService authUserService;
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createPractitioner(Practitioner practitioner) {
		ZBeanUtil.validateBean(practitioner);
		LAuthUser authUser=new LAuthUser();
		authUser.setAccount(practitioner.getAccount());
		authUser.setPassword(practitioner.getPassword());
		authUser.setUserName(practitioner.getName());
		authUser.setDefaultRole(BizConst.ROLE.R_USER);
		authUser.setEnabled(1);
		Long userId=authUserService.createAuthUser(authUser);
		
		
		practitioner.setId(userId);
		
		
//		Object id2=practitionerMapper.selectIdByNumber(practitioner.getNumber());
//		if(id2!=null){
//			ZAlert.Error("执业证号已经存在");
//		}
		practitioner.setEnabled(1);
		practitionerMapper.insertSelective(practitioner);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Practitioner> loadPractitionerById(Long id) {
		if(id!=null){
			return Optional.ofNullable(practitionerMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updatePractitioner(Practitioner practitioner) {
		ZBeanUtil.validateBean(practitioner);
		
		
//		Object id2=practitionerMapper.selectIdByNumber(practitioner.getNumber());
//		if(id2!=null&&id2.equals(practitioner.getId())){
//			ZAlert.Error("执业证号已经存在");
//		}
		practitioner.setEnabled(1);
		practitionerMapper.updateByPrimaryKeySelective(practitioner);
		
		authUserService.updateUserName(practitioner.getId()	, practitioner.getName());
		
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deletPractitioner(Long id) {
		if(id!=null){
			practitionerMapper.deleteByPrimaryKey(id);
			return;
		}
		ZAlert.Error("请选择要删除的数据");
		
	}

	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadPractitionerDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		pm.getObj("number").ifPresent(v->pm.updateParam("number", "%"+v+"%"));
		pm.getObj("orgId").ifPresent(v->pm.updateParam("orgId", v));
		DataSet ds=DataSet.newDS(practitionerMapper.selectListCount(pm), practitionerMapper.selectListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Practitioner> loadByNumber(java.lang.String number){
		if(number!=null){
			return Optional.ofNullable(practitionerMapper.selectByNumber(number));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=false)
	@Override
	public void setEnabled(List<Long> ids, Boolean enabled) {
		ZAssert.notNull(ids, "请选择数据");
		ZAssert.notNull(enabled, "状态参数不能为空");
		int value=enabled?1:0;
		
		ids.forEach(id->{
			practitionerMapper.updateEnabled( id,  value);
			authUserService.updateEnabled(id, value);
		});
		
	}
	

}